#include "Queue.h"


BUFFER_QUEUE str_rx_buffer;				// �������ջ�������UART1
BUFFER_QUEUE ble_rx_buffer;			    // �������ջ�������UART2
BUFFER_QUEUE motor_rx_buffer;			// �������ջ�������UART3


/*************************************************************************************************************************
** ��������:			InitBufferQueue
**
** ��������:			������г�ʼ���������ճ�ʼ�����ȷ����ڴ�ռ䣻
**                      
**                      
**					    
** �������:			struct BUFFER_QUEUE *p_buffer; uint8 length;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-03-07
**------------------------------------------------------------------------------------------------------------------------
** �޶���:				����
** �޶�����:			2009-08-10
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
void InitBufferQueue(BUFFER_QUEUE *p_buffer, uint8 length)
{
	uint8* p_free_space = NULL;
	
	SetReg(p_buffer->state, 0);										// ��ʼ��״̬�Ĵ���
	
	if(0x00 == length)
	{
	//	SetRegMask(p_buffer->state, ERR_MASK, ERR_VALUE);			// ����ʼ�����泤��Ϊ0���ô������ͱ�־λ
		return;
	}
	
	p_buffer->length = length;										// ��ʼ������������
	
	p_free_space = malloc(length);									// ���䳤��Ϊlength��RAM�ռ�
	
	if (NULL == p_free_space)
	{
	//	SetRegMask(p_buffer->state, ERR_MASK, ERR_NOTAVAIL);		// �ô������ͱ�־λ
		return;
	}
	
	p_buffer->init_address = p_free_space;
	
	p_buffer->rear = p_free_space;
	p_buffer->front = p_free_space;
	
	SetRegBit(p_buffer->state, INIT_COMPLETE);						// ��λ��ʼ����ɱ�־
}


/*************************************************************************************************************************
** ��������:			TestEmptyBufferQueue
**
** ��������:			���Ի�������Ƿ�Ϊ�գ�Ϊ���򷵻�TRUE����Ϊ���򷵻�FALSE��
**                      
**                      
**					      
** �������:			struct BUFFER_QUEUE *p_buffer;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-03-07
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
uint8 TestEmptyBufferQueue(BUFFER_QUEUE *p_buffer)
{
	return (p_buffer->rear == p_buffer->front);
}


/*************************************************************************************************************************
** ��������:			AddBufferQueue
**
** ��������:			�򻺳��������Ԫ�أ���βָ��rear��1��
**
**					    
** �������:			struct BUFFER_QUEUE *p_buffer; uint8 &item;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**	
** ������:				����
** ��������:			2009-03-07
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
void AddBufferQueue(BUFFER_QUEUE *p_buffer, uint8 item)
{
	*p_buffer->rear = item;												// �������
	
	p_buffer->rear++;													// ��β��ַ��1
	
	if (p_buffer->rear >= (p_buffer->init_address + p_buffer->length))	// ��β��ַ���������ڴ�߽�
	{
		p_buffer->rear = p_buffer->init_address;						// ��β��ַ�ػ������׵�ַ
	}
	
/*	if (p_buffer->rear == p_buffer->front)								// ���г��ȴ��ڻ���������
	{
		SetRegMask(p_buffer->state, ERR_MASK, ERR_OVERRUN);				// ����������־
	}
*/
}


/*************************************************************************************************************************
** ��������:			OutBufferQueue
**
** ��������:			�Ӷ���ȡ��Ԫ�أ���ͷָ��front��1��������Ϊ��ʱ����FALSE�������ɹ�����TRUE��
**                      
**                      
**					    
** �������:			struct BUFFER_QUEUE *p_buffer; uint8 &item;
** ����ֵ:				uint8;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-03-07
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
uint8 OutBufferQueue(BUFFER_QUEUE *p_buffer, uint8 *p_item)
{
	if(TestEmptyBufferQueue(p_buffer))
	{
		return FALSE;
	}
	else
	{
		*p_item = *p_buffer->front;											// ���ݳ���

		p_buffer->front++;
		
		if(p_buffer->front >= (p_buffer->init_address + p_buffer->length))
		{
			p_buffer->front = p_buffer->init_address;
		}

		return TRUE;
	}
}


/*************************************************************************************************************************
** ��������:			CheckBufferQueueLength
**
** ��������:			��黺����г���;
**
** �������:			struct BUFFER_QUEUE *p_buffer;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-08-19
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
uint8 CheckBufferQueueLength(BUFFER_QUEUE* p_buffer)
{
	uint8 temp_value = 0;

	if (p_buffer->rear >= p_buffer->front)
	{
		temp_value = p_buffer->rear - p_buffer->front;
	}
	else
	{
		temp_value = p_buffer->front - p_buffer->rear;
		temp_value = p_buffer->length - temp_value;
	}

	return temp_value;
}


/*************************************************************************************************************************
** ��������:			CheckBufferQueueState
**
** ��������:			��黺�����״̬�������ڴ�ռ�ʧ�ܡ����������;
**
**
**
** �������:			struct BUFFER_QUEUE *p_buffer;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-08-19
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
uint8 CheckBufferQueueState(BUFFER_QUEUE *p_buffer)
{
	return p_buffer->state;
}


/*************************************************************************************************************************
** ��������:			ClearBufferQueue
**
** ��������:			��ն���;
**
**
** �������:			struct BUFFER_QUEUE *p_buffer;
** ����ֵ:				void;
**
** ʹ�ú����:		None;
** ʹ��ȫ�ֱ���:		None;
**
** ���ú���:			None;
**
** ������:				����
** ��������:			2009-08-19
**------------------------------------------------------------------------------------------------------------------------
** �޶���:
** �޶�����:
**------------------------------------------------------------------------------------------------------------------------
*************************************************************************************************************************/
void ClearBufferQueue(BUFFER_QUEUE *p_buffer)
{
	p_buffer->rear = p_buffer->front;								// �ƶ�ͷβָ��

	SetReg(p_buffer->state, 0);										// ��ʼ��״̬�Ĵ���
	SetRegBit(p_buffer->state, INIT_COMPLETE);						// ��λ��ʼ����ɱ�־
}


/*************************************************************************************************************************
**														�ļ�����
*************************************************************************************************************************/
